package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
import models.Driver;
@Entity
@Table(name="run_card")
public class Run_card extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Vehicle vehicle; //车辆牌照号
	
	public String start_place; //起点
	
	public String end_place; //迄点
	
	public String road_line; //行驶路线及途经地
	
	public Integer sign_number; //标志牌编号
	
	public Integer mileage; //公里数
	
	public Integer person_number; //人次
	
	public Integer mileage_eachperson; //人公里数
	
	public Integer revenue; //营收
	@ManyToOne
	public Driver driver; //驾驶员姓名
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date start_date; //开始日期
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date end_date; //结束日期
	public static Model.Finder<Integer, Run_card> find = new Model.Finder<Integer, Run_card>(Integer.class, Run_card.class);

	public static Page<Run_card> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Run_card> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setStart_place(String d) {
		start_place= d;
	}
	public String getStart_place() {
		return start_place;
	}
	public void setEnd_place(String d) {
		end_place= d;
	}
	public String getEnd_place() {
		return end_place;
	}
	public void setRoad_line(String d) {
		road_line= d;
	}
	public String getRoad_line() {
		return road_line;
	}
	public void setSign_number(Integer d) {
		sign_number= d;
	}
	public Integer getSign_number() {
		return sign_number;
	}
	public void setMileage(Integer d) {
		mileage= d;
	}
	public Integer getMileage() {
		return mileage;
	}
	public void setPerson_number(Integer d) {
		person_number= d;
	}
	public Integer getPerson_number() {
		return person_number;
	}
	public void setMileage_eachperson(Integer d) {
		mileage_eachperson= d;
	}
	public Integer getMileage_eachperson() {
		return mileage_eachperson;
	}
	public void setRevenue(Integer d) {
		revenue= d;
	}
	public Integer getRevenue() {
		return revenue;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setStart_date(Date d) {
		start_date= d;
	}
	public Date getStart_date() {
		return start_date;
	}
	public void setEnd_date(Date d) {
		end_date= d;
	}
	public Date getEnd_date() {
		return end_date;
	}
	public static Run_card findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByVehicle(Vehicle d) {
		return find.where().eq("vehicle", d).findUnique();
	}
	public static String getVehicleJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getVehicle().toString(), v.getVehicle().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByStart_place(String d) {
		return find.where().eq("start_place", d).findUnique();
	}
	public static String getStart_placeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getStart_place().toString(), v.getStart_place().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByEnd_place(String d) {
		return find.where().eq("end_place", d).findUnique();
	}
	public static String getEnd_placeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getEnd_place().toString(), v.getEnd_place().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByRoad_line(String d) {
		return find.where().eq("road_line", d).findUnique();
	}
	public static String getRoad_lineJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getRoad_line().toString(), v.getRoad_line().toString()); 
		}
		return node.toString();
	}
	public static Run_card findBySign_number(Integer d) {
		return find.where().eq("sign_number", d).findUnique();
	}
	public static String getSign_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getSign_number().toString(), v.getSign_number().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByMileage(Integer d) {
		return find.where().eq("mileage", d).findUnique();
	}
	public static String getMileageJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getMileage().toString(), v.getMileage().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByPerson_number(Integer d) {
		return find.where().eq("person_number", d).findUnique();
	}
	public static String getPerson_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getPerson_number().toString(), v.getPerson_number().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByMileage_eachperson(Integer d) {
		return find.where().eq("mileage_eachperson", d).findUnique();
	}
	public static String getMileage_eachpersonJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getMileage_eachperson().toString(), v.getMileage_eachperson().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByRevenue(Integer d) {
		return find.where().eq("revenue", d).findUnique();
	}
	public static String getRevenueJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getRevenue().toString(), v.getRevenue().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByDriver(Driver d) {
		return find.where().eq("driver", d).findUnique();
	}
	public static String getDriverJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getDriver().toString(), v.getDriver().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByStart_date(Date d) {
		return find.where().eq("start_date", d).findUnique();
	}
	public static String getStart_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getStart_date().toString(), v.getStart_date().toString()); 
		}
		return node.toString();
	}
	public static Run_card findByEnd_date(Date d) {
		return find.where().eq("end_date", d).findUnique();
	}
	public static String getEnd_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Run_card v: find.all()) {
			node.put(v.getEnd_date().toString(), v.getEnd_date().toString()); 
		}
		return node.toString();
	}
}
